using System;
using System.Collections.Generic;
using System.Linq;
using C3.Proto.Core.Model;
using C3.Proto.Core.Repository;
using SubSonic.DataProviders;

namespace C3.Proto.Repo
{
    public class SafetyRepo : RepositoryBase, ISafetyRepo
    {
        public IList<Inspection> GetMonthlyInspections(DateTime start, DateTime end)
        {
            return repo.Find<Inspection>(t=> t.inspectionDate >= start && t.inspectionDate < end);
        }

        public IList<Claim> GetClaims(DateTime start, DateTime end)
        {
            var r = repo.Find<Claim>(t => t.DateofLoss >= start && t.DateofLoss < end);
            return r;
        }

        public IList<Inspection> GetRegionalMonthlyInspections(DateTime start, DateTime end, string region)
        {
            return repo.Find<Inspection>(t => t.inspectionDate >= start && t.inspectionDate < end && t.projectRegion == region);
        }

        public IList<Step> GetSteps(string project, DateTime start, DateTime end)
        {
            return repo.Find<Step>(t => t.ProjectId == project && t.Date >= start && t.Date < end);
        }

        public IList<InspectionSummary> GetMonthlyInspectionSummaries(DateTime start, DateTime end)
        {
            return repo.Find<InspectionSummary>(t => t.inspectionDate >= start && t.inspectionDate < end);
        }

        public RIR GetRIR(string project, DateTime start, DateTime end)
        {
            var date = end.AddMonths(-1);
            return repo.Single<RIR>(t => t.projectJobNumber == project && t.Date == date);
        }

        public IList<LaborHour> GetLabor(string project, DateTime start, DateTime end)
        {
            return repo.Find<LaborHour>(t => t.projectJobNumber == project && t.Date >= start && t.Date < end);
        }

        public IList<ManHour> GetManHours(string project, DateTime start, DateTime end)
        {
            return repo.Find<ManHour>(t => t.Project == project && t.Date >= start && t.Date < end);
        }

        public IList<JobHistory> GetJobHistory()
        {
            var sproc = new SubSonic.Schema.StoredProcedure("GetJobHistory", ProviderFactory.GetProvider("DBO2Safety"));
            return sproc.ExecuteTypedList<JobHistory>();
        }

        public IList<StepError> GetStepErrors()
        {
            var sproc = new SubSonic.Schema.StoredProcedure("GetStepErrorsForCurrentPeriod", ProviderFactory.GetProvider("DBO2Safety"));
            return sproc.ExecuteTypedList<StepError>();
        }

        public IList<Project> GetProjects()
        {
            return repo.All<Project>().ToList();
        }

        public Project GetProjectByName(string name)
        {
            return repo.Single<Project>(t => t.Name == name);
        }

        public OSHAAverage GetOSHAAverage(int year)
        {
            year = year - 2;   
            return repo.Single<OSHAAverage>(t => t.Year == year);
        }
    }
}